<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <!-- <el-form-item label="负&nbsp;&nbsp;&nbsp;责人" prop="adminId">
        <el-select v-model="queryParams.adminId" placeholder="请输入项目负责人" clearable class="!w-240px">
          <el-option v-for="item in userList" :key="item.id" :label="item.nickname" :value="item.id" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="项目描述" prop="description">
        <el-input v-model="queryParams.description" placeholder="请输入项目描述" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="项目状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择项目状态" class="!w-240px">
          <el-option v-for="dict in getDictOptions(DICT_TYPE.BPM_PROJECT_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="开始时间" prop="startTime">
        <el-date-picker v-model="queryParams.startTime" value-format="x" type="datetime" placeholder="选择项目开始时间"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker v-model="queryParams.endTime" value-format="x" type="datetime" placeholder="选择项目结束时间"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="项目标签" prop="tagId">
        <el-tree-select v-model="queryParams.tagId" placeholder="请选择标签" :data="options" :render-after-expand="false"
          class="!w-240px" />
      </el-form-item> -->

      <el-form-item>
        <el-button @click="handleQuery" v-hasPermi="['bpm:project-info:query']">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['bpm:project-info:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['bpm:project-info:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="项目名称" align="center" prop="name" />
      <!-- <el-table-column label="负责人" align="center" prop="adminId" /> -->
      <el-table-column label="项目描述" align="center" prop="description" />
      <el-table-column label="项目状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROJECT_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="项目开始时间" align="center" prop="startTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="项目结束时间" align="center" prop="endTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="标签id" align="center" prop="tagId" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['bpm:project-info:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['bpm:project-info:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ProjectInfoForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ProjectInfoApi, ProjectInfoVO } from '@/api/bpm/projectinfo'
import ProjectInfoForm from './ProjectInfoForm.vue'
import { DICT_TYPE, getDictOptions, getIntDictOptions } from '@/utils/dict'
import { TagGroupInfoApi, TagGroupInfoVO } from '@/api/bpm/taggroupinfo'
import * as UserApi from '@/api/system/user'
import { useRoute } from 'vue-router';
// import router from '@/router'


/** 流程项目信息 列表 */
defineOptions({ name: 'BpmProjectInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProjectInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  adminId: undefined,
  description: undefined,
  status: undefined,
  startTime: [],
  endTime: [],
  tagId: undefined,
  order: undefined,
  remark: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

//标签组下拉选
const options = ref([]);

//负责人下拉选
const userList = ref<UserApi.UserVO[]>([]) // 用户列表

const getAllTagInfo = async () => {
  //打开弹窗获取标签组数据
  let response = await TagGroupInfoApi.getAllTagGroupInfo();
  options.value = response.map(tagGroup => ({
    value: tagGroup.id,
    label: tagGroup.name,
    children: tagGroup.children.map(tag => ({
      value: tag.tagId,
      label: tag.tagName
    }))
  }));
}
const getUserList = async () => {
  // 获得用户列表
  userList.value = await UserApi.getSimpleUserList()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectInfoApi.getProjectInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProjectInfoApi.deleteProjectInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProjectInfoApi.exportProjectInfo(queryParams)
    download.excel(data, '流程项目信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const route = useRoute() // 路由

/** 初始化 **/
onMounted(() => {
  getAllTagInfo();
  getUserList();
  if (route.query.id) {
    queryParams.status = route.query.id
    getList()
  } else {
    getList();
  }

})
</script>